package com.awesome.common.base.mvp

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.awesome.common.util.ui.StatusBarUtil
import java.lang.reflect.ParameterizedType

/**
 * Author: JFangZ
 * Email: janezhang1901@163.com
 * Date:2021/4/8 11:32
 * Description:The base of fragment.If want to use MVP frame,please inherit this.
 */
abstract class AbsMVPFragment<V : IBaseView, P : IPresenter<V>> : Fragment(), IBaseView {
    protected var presenter: P? = null

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:32
     * Description:Provided layoutId to setContentView()
     * @return The layoutId of this activity.
     */
    abstract fun initLayoutId(): Int

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:32
     * Description:Do UI init operation,such as findViewByID,setAdapter and so on.
     */
    abstract fun initView()

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:32
     * Description:Do setListener/setCallback operation.
     */
    abstract fun setListener()

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2019/4/8 11:34
     * Description:Do data init operation,such as network request,read database,read file and so on.
     */
    abstract fun initData()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (immersive()) {
            // 使内容延伸到状态栏下
            StatusBarUtil.setStatusBarTranslucent(activity?.window, true)
            // 使状态栏透明
            StatusBarUtil.setStatusBarColor(activity?.window, Color.TRANSPARENT, false)
        } else {
            StatusBarUtil.setStatusBarTranslucent(activity?.window, false)
            StatusBarUtil.setStatusBarColor(activity?.window, initStatusBarColor(), false)
        }
        // 状态栏深色图标
        StatusBarUtil.setStatusBarStyle(activity?.window, initStatusBarDark())
        return initLayoutView(container)

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        presenter = createPresenter()?.also { it.attachView(this as V) }
        initView()
        setListener()
        initData()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            if (immersive()) {
                // 使内容延伸到状态栏下
                StatusBarUtil.setStatusBarTranslucent(activity?.window, true)
                // 使状态栏透明
                StatusBarUtil.setStatusBarColor(activity?.window, Color.TRANSPARENT, false)
            } else {
                StatusBarUtil.setStatusBarTranslucent(activity?.window, false)
                StatusBarUtil.setStatusBarColor(activity?.window, initStatusBarColor(), false)
            }
            // 状态栏深色图标
            StatusBarUtil.setStatusBarStyle(activity?.window, initStatusBarDark())
        }
    }

    private fun createPresenter(): P? {
        try {
            val clazz = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1] as Class<P>
            return clazz.newInstance()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    open fun initLayoutView(container: ViewGroup?): View {
        return LayoutInflater.from(context).inflate(initLayoutId(), container, false)
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:39
     * Description:Make the activity immersive or not,if enable immersion,the status bar will transparent.
     * @return Returns true to enable immersion,false not to enable immersion.
     */
    open fun immersive(): Boolean = false

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:44
     * Description:Set the color of status bar.
     * @return Color's value.
     */
    open fun initStatusBarColor(): Int = 0xFF000000.toInt()

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:45
     * Description:Set the mode of icon in status bar
     * @return Returns true to start dark mode,false to start light mode.
     */
    open fun initStatusBarDark(): Boolean = false

    fun <T : View> findViewById(id: Int): T = view?.findViewById(id) as T

    open fun toastShort(content: String) = Toast.makeText(context, content, Toast.LENGTH_SHORT).show()

    open fun toastLong(content: String) = Toast.makeText(context, content, Toast.LENGTH_LONG).show()

    fun finish() = activity?.finish()
}
